package com.tedu.servies;

import com.tedu.mapper.UserMapper;
import com.tedu.pojo.User;
import net.sf.jsqlparser.util.deparser.InsertDeParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户模块的服务层接口实现类
 * 调用mapper接口对象
 * 交给spring管理
 */
@Service   // 用户服务对象交给spring管理
public class UserServiesImpl implements UserServies {
    @Autowired
    UserMapper userMapper;

    public List<User> login(String name, String pwd) {
        //把参数name+pwd 添加到map对象，使用map对象进行查询

        Map<String ,Object> map = new HashMap<String, Object>();
        map.put("username",name);
        map.put("password",pwd);

        List<User> users = userMapper.selectByMap(map);

        System.out.println("登录成功的用户："+users);
        return users;
    }



//    public List<User> zhuce(User user) {
//        Map<String ,Object> map = new HashMap<String, Object>();
//        map.put("username",user.getUsername());
//
//        List<User> zhuces = userMapper.selectByMap(map);
//        if(!zhuces.isEmpty()){
//            System.out.println("用户名已存在");
//        }
//        String md5Password = getMD5Password(user.getPassword());
//        user.setPassword(md5Password);
//        user.setCreated(new Date());
//        user.setUpdated(new Date());
//        int rows = userMapper.insert((user));
//        if(rows!=1){
//            System.out.println("注册失败");
//        }
//        return zhuces;
//    }

    public int register(User user){
        user.setCreated(new Date());
        int rows = userMapper.insert((user));

        return  rows;
    }

    public int modify(User user){
        user.setUpdated(new Date());
        int sc = userMapper.updateById(user);
        return sc;
    }


    public String getMD5Password(String password){
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        return  password;
    }

}
